package vertx;

import io.vertx.core.AbstractVerticle;
import io.vertx.core.Promise;
import io.vertx.ext.web.Router;

public class RouterVerticle extends AbstractVerticle {
  //第一步 声明路由
  Router router;

  @Override
  public void start(Promise<Void> startPromise) throws Exception {
    //第二部 初始化router
   router =Router.router(vertx);
   //第三步 将Router与vertx HttpServer 绑定

    //第四步配置解析Url 改成get 只能接受get请求
    router.get("/").handler(
      req -> {
        req.response()
          //text/plain修改json格式application/json
          .putHeader("content-type", "application/json")
          .end("Hello from Vert.x!");
      });


    vertx.createHttpServer().requestHandler(router).listen(8888,http->{
      if (http.succeeded()) {
        startPromise.complete();
        System.out.println("HTTP server started on port 8888");
      } else {
        startPromise.fail(http.cause());
      }
    });
  }
}
